package cn.liuxue.lifecircle.camera.views;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import cn.liuxue.lifecircle.FamilyApplication;
import cn.liuxue.lifecircle.R;
import cn.liuxue.lifecircle.camera.CameraPreview;
import cn.liuxue.lifecircle.util.SystemUtil;

@SuppressLint("NewApi")
public class CameraActivity extends Activity {

	private Camera mCamera;
	private CameraPreview mPreview;
	private String TAG = "CameraActivity";
	private FamilyApplication mApplication;
	private FrameLayout preview;
	private Button previewButton;
	private Button captureButton;
	private static final DelayQueue<Timeout> TIMEOUT_QUEUE = new DelayQueue<Timeout>();

	
	public static class Timeout implements Delayed {

		@Override
		public int compareTo(Delayed arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public long getDelay(TimeUnit unit) {
			// TODO Auto-generated method stub
			return 0;
		}
		
	}
	
	
	
	private PictureCallback mPicture = new PictureCallback() {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			Bitmap bp = BitmapFactory.decodeByteArray(data, 0, data.length);
			showImage(bp);
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		Log.d(TAG, "==onConfigurationChanged==");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.camara);
//		if (!checkCameraHardware(this)) {
//			SystemUtil.showNetworkError(this);
//		}
//		mApplication = (FamilyApplication) getApplication();
//		preview = (FrameLayout) findViewById(R.id.camera_preview);
//		// get an image from the camera
//		// Create an instance of Camera
//		mCamera = getCameraInstance();
//		// Create our Preview view and set it as the content of our activity.
//		mPreview = new CameraPreview(this, mCamera);
//		// Add a listener to the Capture button
//		previewButton = (Button) findViewById(R.id.button_preview);
//		captureButton = (Button) findViewById(R.id.button_capture);
//
//		captureButton.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				// get an image from the camera
//				setCameraDisplayOrientation(0,mCamera);
//				mCamera.takePicture(null, null, mPicture);
//				captureButton.setVisibility(View.INVISIBLE);
//				previewButton.setVisibility(View.VISIBLE);
//			}
//		});
//
//		previewButton.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//
//				preview.removeAllViews();
//				preview.addView(mPreview);
//				captureButton.setVisibility(View.VISIBLE);
//				previewButton.setVisibility(View.INVISIBLE);
//			}
//		});

	}

	private void showImage(Bitmap bm) {
		preview.removeAllViews();
		ImageView iv = new ImageView(this);
		iv.setImageBitmap(bm);
		preview.addView(iv);

	}

	/** Check if this device has a camera */
	public boolean checkCameraHardware(Context context) {
		if (context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_CAMERA)) {
			// this device has a camera
			return true;
		} else {
			// no camera on this device
			return false;
		}
	}

	/** A safe way to get an instance of the Camera object. */
	public static Camera getCameraInstance() {
		Camera c = null;
		try {
			c = Camera.open(); // attempt to get a Camera instance
		} catch (Exception e) {
			// Camera is not available (in use or does not exist)
		}
		return c; // returns null if camera is unavailable
	}

	private void setCameraDisplayOrientation(int cameraId,
			android.hardware.Camera camera) {
		android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
		android.hardware.Camera.getCameraInfo(cameraId, info);
		int rotation = getWindowManager().getDefaultDisplay().getRotation();
		int degrees = 0;
		switch (rotation) {
		case Surface.ROTATION_0:
			degrees = 0;
			break;
		case Surface.ROTATION_90:
			degrees = 90;
			break;
		case Surface.ROTATION_180:
			degrees = 180;
			break;
		case Surface.ROTATION_270:
			degrees = 270;
			break;
		}

		int result;
		if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
			result = (info.orientation + degrees) % 360;
			result = (360 - result) % 360; // compensate the mirror
		} else { // back-facing
			result = (info.orientation - degrees + 360) % 360;
		}
		camera.setDisplayOrientation(result);
	}

}
